home *** CD-ROM | disk | FTP | other *** search
/ The Arsenal Files 8 / The Arsenal Files Collection #8 (Arsenal Computer) (1996).ISO / prg_casm / snpd9611.zip / SETLEVEL.C < prev    next >
C/C++ Source or Header  |  1996-11-24  |  10KB  |  273 lines

  1. /* +++Date last modified: 09-Nov-1996 */
  2.  
  3. /********************************************************************
  4.  *                         SETLEVEL                                 *
  5.  *                                                                  *
  6.  * Written by: Lynn R. Lively                                       *
  7.  * Date:       05/15/90                                             *
  8.  * Version:    1.0                                                  *
  9.  * Written in TurboC V2.00                                          *
  10.  *                                                                  *
  11.  * Allows user input in a MSDOS .BAT file. This program asks a      *
  12.  * specified question and sets the errorlevel according to valid    *
  13.  * answer list. Since MSDOS provides no facility for getting user   *
  14.  * input into a .BAT file, and manipulation of enviorinment strings *
  15.  * seems to be unreliable and nonportable, I have written this      *
  16.  * facility as a possible alternative.                              *
  17.  *                                                                  *
  18.  * I hereby place this program into the public domain.              *
  19.  * Users of this utility do so at their own risk. I accept no       *
  20.  * responsibility for the accuracy or useability of this software.  *
  21.  * However, should you have any questions, suggestions, or problems *
  22.  * I would be happy to talk with you (and help if I can).           *
  23.  * My current work number is 713-591-6111                           *
  24.  *  Your Servant,                                                   *
  25.  *       Lynn R. Lively                                             *
  26.  *                                                                  *
  27.  ********************************************************************/
  28.  
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <conio.h>
  33.  
  34. /*
  35.  * The following are bit mask values for the option_flg.
  36.  */
  37.  
  38. #define CASE_SENSE       1
  39. #define SINGLE_KEY_ENTRY 2
  40.  
  41. /*
  42.  * The following is the structure deffinition for the valid answer
  43.  * linked list.
  44.  */
  45.  
  46. typedef struct v_ans {
  47.       char *         ans_key;
  48.       int            rtn_err_lev;
  49.       struct v_ans * next_ans;
  50. } VALID_ANS;
  51.  
  52. /*
  53.  * Prototypes of functions defined in this module.
  54.  */
  55.  
  56. void usage (char * progname);
  57.  
  58. int main (int argc, char * argv[])
  59. {
  60.       register int i = 0;
  61.       register int j;
  62.  
  63.       int option_flg = 0;
  64.  
  65.       VALID_ANS * answer_tab = NULL;
  66.       VALID_ANS * wk_ans;
  67.  
  68.       char * wk_ptr;
  69.       char * ques_ptr = "?";
  70.       char   wk_str[256];
  71.  
  72.       if (argc > 1)
  73.       {
  74.         /*
  75.          * Step through and parse the argument list.
  76.          */
  77.  
  78.             for (i = 1; i < argc; i++)
  79.             {
  80.                   j = 0;
  81.                   switch (argv[i][j])
  82.                   {
  83.                   case '/':            /* Options are flagged by '/' */
  84.  
  85.                         j++;
  86.                         switch (argv[i][j])
  87.                         {
  88.                         case 'c':
  89.                         case 'C':
  90.  
  91.                               option_flg |= CASE_SENSE;
  92.                               break;
  93.  
  94.                         case '1':
  95.  
  96.                               option_flg |= SINGLE_KEY_ENTRY;
  97.                               break;
  98.  
  99.                         case 'q':
  100.                         case 'Q':
  101.  
  102.                         /*
  103.                          * The question is assumed to be the argument
  104.                          * after the 'q'.
  105.                          */
  106.  
  107.                               ques_ptr = strdup (argv[++i]);
  108.                               break;
  109.  
  110.                         default:
  111.  
  112.                         /*
  113.                          * Oops! Something wrong. Tell them how its
  114.                          * supposed to work.
  115.                          */
  116.  
  117.                               usage (argv[0]);
  118.                               return (-1);
  119.                         }
  120.                         break;
  121.  
  122.                         case '-':         /* Answers are flagged by '-' */
  123.  
  124.                         /*
  125.                          * The answer is assumed to be everything from the
  126.                          * '-' to the '=' characters. This allows for
  127.                          * single letter and word answers.
  128.                          */
  129.  
  130.                               j++;
  131.                               if ((wk_ptr = strtok (argv[i]+j, "=")) != NULL)
  132.                               {
  133.                               /*
  134.                               * Allocate space for the new answer entry and
  135.                               * link in into the list. Answers are linked
  136.                               * into the list in reverse order from their
  137.                               * command line specification since the logic
  138.                               * is considerably simpler and it shouldn't
  139.                               * matter much anyway.
  140.                               */
  141.  
  142.                                     if (answer_tab == NULL)
  143.                                     {
  144.                                           answer_tab = wk_ans = (VALID_ANS *)
  145.                                                 calloc (1, sizeof(VALID_ANS));
  146.                                     }
  147.                                     else
  148.                                     {
  149.                                           wk_ans = (VALID_ANS *)
  150.                                                 calloc (1, sizeof(VALID_ANS));
  151.                                           wk_ans->next_ans = answer_tab;
  152.                                           answer_tab = wk_ans;
  153.                                     }
  154.  
  155.                               /*
  156.                               * Store the answer string and errorlevel value
  157.                               * into the answer element.
  158.                               */
  159.  
  160.                                     wk_ans->ans_key = strdup (wk_ptr);
  161.                                     wk_ptr          = strtok (NULL, "=");
  162.                                     if (wk_ptr != NULL)
  163.                                     {
  164.                                     /*
  165.                                     * Allow only positive returns since -1 is
  166.                                     * and error return from the program.
  167.                                     */
  168.  
  169.                                           wk_ans->rtn_err_lev =
  170.                                                 abs (atoi (wk_ptr));
  171.                                     }
  172.                                     else  wk_ans->rtn_err_lev = 0;
  173.                               }
  174.                         }
  175.                         break;
  176.  
  177.                   default:
  178.  
  179.                   /*
  180.                    * Oops! Something wrong. Tell them how its
  181.                    * supposed to work.
  182.                    */
  183.  
  184.                         usage (argv[0]);
  185.                         return (-1);
  186.                   }
  187.             }
  188.       }
  189.       else
  190.       {
  191.  
  192.             /*
  193.              * Oops! Something wrong. Tell them how its
  194.              * supposed to work.
  195.              */
  196.  
  197.             usage (argv[0]);
  198.             return (-1);
  199.       }
  200.  
  201.       /*
  202.        * Ask the question and get their answer.
  203.        */
  204.  
  205.       printf ("%s", ques_ptr);
  206.  
  207.       if ((option_flg & SINGLE_KEY_ENTRY) != 0)
  208.       {
  209.             wk_str[0] = getche ();
  210.             wk_str[1] = '\0';
  211.       }
  212.       else  fgets (wk_str, sizeof (wk_str), stdin);
  213.  
  214.       /*
  215.        * See if we can find their answer in our valid answer list.
  216.        */
  217.  
  218.       wk_ans = answer_tab;
  219.       while (wk_ans != (VALID_ANS *) NULL)
  220.       {
  221.             if ((option_flg & CASE_SENSE) != 0)
  222.             {
  223.                   if ((strncmp (wk_str, wk_ans->ans_key,
  224.                         (strlen (wk_ans->ans_key)))) == 0)
  225.                   {
  226.                   /*
  227.                    * If we found the answer return the associated
  228.                    * errorlevel return number.
  229.                    */
  230.  
  231.                         return (wk_ans->rtn_err_lev);
  232.                   }
  233.             }
  234.             else
  235.             {
  236.                   if ((strnicmp (wk_str, wk_ans->ans_key,
  237.                         (strlen (wk_ans->ans_key)))) == 0)
  238.                   {
  239.                   /*
  240.                    * If we found the answer return the associated
  241.                    * errorlevel return number.
  242.                    */
  243.  
  244.                         return (wk_ans->rtn_err_lev);
  245.                   }
  246.             }
  247.  
  248.             wk_ans = wk_ans->next_ans;
  249.       }
  250.  
  251.       /*
  252.        * If the answer wasn't in our table return a -1.
  253.        */
  254.  
  255.       return (-1);
  256. }
  257.  
  258. void usage (char * progname)
  259. {
  260.       printf ("%s usage:\n", progname);
  261.       printf ("%s /[options] -<valid answer>=<errorlevel>\n\n", progname);
  262.       printf ("Where:\n");
  263.       printf ("  options      = c    (Answers are case sensitive)\n");
  264.       printf ("                 1    (Single key entry)\n");
  265.       printf ("                 q\"Question Text\"\n\n");
  266.       printf ("  valid answer = (Single letter or word response)\n");
  267.       printf ("  errorlevel   = (errorlevel to return when answer seen)\n\n");
  268.       printf ("Example:\n%s /q\"Answer Y or N: \" /1 -y=1 -n=2\n\n",
  269.             progname);
  270.       printf ("Note: -1 is returned if answer is not valid or syntax\n");
  271.       printf ("      is incorrect.\n");
  272. }
  273.